$.get('/static/admin/lib/webuploader/webuploader.js');
layui.link('/static/admin/lib/webuploader/webuploader.css');
layui.define(['jquery', 'layer','element'], function (exports) {
    
    
    
    var SingleUpload = function(param){   
        var uploader = WebUploader.create({
            swf     : param.swf,
            server  : param.url,
            pick    : {
                id : param.hideBtn
            },
            fileVal : 'file',//隐藏域name
            auto: param.auto,            
            chunked: param.chunked,  // 开起分片上传。
            accept: param.accept,
            duplicate: true // 允许同一个文件多次上传
        });
        
        uploader.on( 'fileQueued', function( file ) {
            uploader.md5File( file ).then(function(val) {
                file.md5str = val;
            });
            
		});
        // 文件准备上传之前触发
        uploader.on('uploadStart',function( file) {
            console.log( 'uploadStart');        
            var now = new Date();    
            var hour = now.getHours().toString();//得到小时
            var minu = now.getMinutes().toString();//得到分钟
            var sec  = now.getSeconds().toString();//得到秒
            var MS   = now.getMilliseconds().toString();//获取毫秒
            //console.log(hour+minu+sec+MS);
            file.fileid = hour+minu+sec+MS;        
            
            var html = '<div id = "JS_webuploader_progress" style=" width: 100%; position: fixed;z-index: 999;top: 1px;">'
                       +'<div lay-filter="webuploder"  class="layui-progress" lay-showpercent="true">'
                       +'     <div class="layui-progress-bar" lay-percent="20%"></div>'
                       +' </div></div>';
            $('body').prepend(html);
        });
        //每片 文件上传时触发
        uploader.on('uploadBeforeSend',function(object ,data,headers){
            data.fileid = object.file.fileid;
        });
        //每片 文件上传完触发
        uploader.on('uploadAccept',function(object ,ret){
            console.log('uploadAccept');
            //console.log( object );
            var retdata = ret.data;
            object.file.retdata = retdata;
        });
        
        // 上传进度
        uploader.on('uploadProgress',function(file ,percentage){
            layui.element.progress('webuploder', percentage*100+'%');
        }); 
                
        // 文件上传完成 通知合并 uploadComplete
        uploader.on('uploadComplete',function(file ){
            $("#JS_webuploader_progress").remove();
            //console.log( 'uploadComplete' );
            var retdata = file.retdata;
            if( typeof retdata == 'undefined'){
                param.error('文件上传失败');
                return false;
            }                
            if( retdata.status == 'success' ){
                
                if(typeof retdata.md5 != 'undefined' 
                    && typeof file.md5str != 'undefined' 
                    && retdata.md5 != file.md5str ){
                    param.error('文件上传失败[内容校验错误]');
                    return false;
                }
                
                param.done(retdata);
            }else if( retdata.status == 'error' ){
                param.error(retdata.error);
                return false;
            }else{
                // 通知合并
            }            
        });
        
        uploader.on('error',function(type){
            console.log( 'error:'+type); 
        });
    }
    
    var fileUploader = {                
        render:function(e){
            $.extend(config,e);            
            var hideBtn = config.elem.replace(/[#\.]/,"")+'hideWebuploaderBtn';
            $(config.elem).after('<div style="display: none;" id="'+hideBtn+'"></div>');
            config.hideBtn = '#'+hideBtn;
            $(config.elem).on('click',function(){
                $(config.hideBtn).find('input[name="file"]').click(); 
            });
            SingleUpload(config);
        }
        
    }
    
    var config = {
        'elem'     : '',
        'done'     :function(ret){},
        'error'   :function(error){
                console.log( 'error:'+error );
            },
        'url'      : '/test/upload/upfile?filetype=terminal',
        'swf'      : '/static/js/admin/lib/webuploader/webuploader/Uploader.swf',
        'fileNumLimit'  : 1,
        'fileSizeLimit' : 1048576,
        'auto'      : true,
        'chunked'   : true,
        'accept':{
			  'title':'Images',
			  'extensions': '*',
			  'mimeTypes': '*'
		   }
    }
    
    exports('webuploader', fileUploader);
    
});